/**
 * Copyright (C) 2013-2014 the original author or authors.
 */
package com.foreveross.bsl.common.utils.ws.jackson.mixins;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Sort;

import com.fasterxml.jackson.annotation.JsonIgnore;

/**
 * xxx
 * 
 * @author Wangyi
 * @version v1.0
 * 
 * @date 2013-7-25
 * 
 */
public class SimplePage<T> implements Page<T> {

	private List<T> content = new ArrayList<T>();
	private long totalElements;
	private int number;
	private int size;
	private Sort sort;

	public int getNumber() {
		return number;
	}

	public int getSize() {
		return size;
	}

	@JsonIgnore
	public int getTotalPages() {
		return getSize() == 0 ? 0 : (int) Math.ceil((double) totalElements / (double) getSize());
	}

	@JsonIgnore
	public int getNumberOfElements() {
		return content.size();
	}

	public long getTotalElements() {
		return this.totalElements;
	}

	public boolean hasPreviousPage() {
		return getNumber() > 0;
	}

	@JsonIgnore
	public boolean isFirstPage() {
		return !hasPreviousPage();
	}

	public boolean hasNextPage() {
		return ((getNumber() + 1) * getSize()) < this.totalElements;
	}

	@JsonIgnore
	public boolean isLastPage() {
		return !hasNextPage();
	}

	public Iterator<T> iterator() {
		return content.iterator();
	}

	public List<T> getContent() {
		return Collections.unmodifiableList(content);
	}

	public boolean hasContent() {
		return !content.isEmpty();
	}

	public Sort getSort() {
		return sort;
	}

	/**
	 * @param content
	 *            the content to set
	 */
	public void setContent(List<T> content) {
		this.content = content;
	}

	/**
	 * @param totalElements
	 *            the totalElements to set
	 */
	public void setTotalElements(long totalElements) {
		this.totalElements = totalElements;
	}

	/**
	 * @param number
	 *            the number to set
	 */
	public void setNumber(int number) {
		this.number = number;
	}

	/**
	 * @param size
	 *            the size to set
	 */
	public void setSize(int size) {
		this.size = size;
	}

	/**
	 * @param sort
	 *            the sort to set
	 */
	public void setSort(Sort sort) {
		this.sort = sort;
	}

}
